home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gxx / gpincl20.zoo / xallocri.h < prev    next >
C/C++ Source or Header  |  1993-07-13  |  2KB  |  78 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1989 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19.  
  20. #ifndef _AllocRing_h
  21. #ifdef __GNUG__
  22. #pragma interface
  23. #endif
  24. #define _AllocRing_h 1
  25.  
  26.  
  27. /*
  28.  * dl/per: the typing for sizes in this entire lib really sucks man!
  29.  * it really screws us on the atari where:
  30.  *    size_t != long != (necessarily) int != short
  31.  *
  32.  * yeah, yeah, i know i should'nt be complaining free software and all that
  33.  * jazz, but this is even asthetically displeasing: having a signed type for
  34.  * sizes firstly restricts the size (try int == 16 bits), and secondly
  35.  * it cuts out the possibility of catching silly error where the size
  36.  * is or becomes negative.
  37.  *
  38.  *  ++jrb
  39.  */
  40.  
  41.  
  42. /*
  43.   An AllocRing holds the last n malloc'ed strings, reallocating/reusing 
  44.   one only when the queue wraps around. It thus guarantees that the
  45.   last n allocations are intact. It is useful for things like I/O
  46.   formatting where reasonable restrictions may be made about the
  47.   number of allowable live allocations before auto-deletion.
  48. */
  49.  
  50. class AllocRing
  51. {
  52.  
  53.   struct AllocQNode
  54.   {
  55.     void*  ptr;
  56.     size_t sz;
  57.   };
  58.  
  59.   AllocQNode* nodes;
  60.   size_t      n;
  61.   size_t      current;
  62.  
  63.   long        find(void* p);
  64.  
  65. public:
  66.  
  67.               AllocRing(size_t max);
  68.              ~AllocRing();
  69.  
  70.   void*       alloc(size_t size);
  71.   int         contains(void* ptr);
  72.   void        clear();
  73.   void        free(void* p);
  74. };
  75.  
  76.  
  77. #endif
  78.